<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$party       = array();
$fineTotal   = 0;
$amountTotal = 0;

$selectParty = "SELECT partyId,partyName FROM party
                 ORDER BY partyName";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
	$party[$a]['id']     = $partyRow['partyId'];
	$party[$a]['name']   = $partyRow['partyName'];
	$party[$a]['fine']   = 0;
	$party[$a]['amount'] = 0;
	
	
  $selectVeepariBook = "SELECT vepaaribilldetail.fine,vepaaribilldetail.amount,vepaaribilldetail.CrDr,vepaaribillmaster.PartyId
	                        FROM vepaaribilldetail
	                        JOIN vepaaribillmaster ON vepaaribilldetail.vepaariBookMasterId = vepaaribillmaster.vepaariBookMasterId 
	                       WHERE vepaaribillmaster.partyId = ".$partyRow['partyId']."
                           AND vepaariBookDate <= '".date("Y-m-d")."'
	                       ORDER BY vepaariBookDate, vepaariBookDetailId";
	$selectVeepariBookRes = mysql_query($selectVeepariBook);
	while($row = mysql_fetch_assoc($selectVeepariBookRes))
	{
		if($row['CrDr'] == "Dr")
		{
			$party[$a]['fine']   += $row['fine'];
	    $party[$a]['amount'] += $row['amount'];
		}
		else
		{
			$party[$a]['fine']   -= $row['fine'];
	    $party[$a]['amount'] -= $row['amount'];
		}
	}
	
	$billBook = "SELECT totalFine,totalAmount FROM billbook
	              WHERE partyId = ".$partyRow['partyId']."
	              ORDER BY billBookId";
	$billBookRes = mysql_query($billBook);
	while($billBook = mysql_fetch_assoc($billBookRes))
	{
		$party[$a]['fine']   += $billBook['totalFine'];
    $party[$a]['amount'] += $billBook['totalAmount'];
	}
	
	$fineTotal   += $party[$a]['fine'];
	$amountTotal += $party[$a]['amount'];
	$a++;
}

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_partyName",$l_partyName);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_total",$l_total);

$smarty->assign("party",$party);
$smarty->assign("fineTotal",$fineTotal);
$smarty->assign("amountTotal",$amountTotal);
$smarty->display("vepaariSummery.tpl");
?>